﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Workbench.ViewModels;
using System.Diagnostics;

namespace WorkbenchTests
{
    [TestClass]
    public class BuildViewModelTests
    {
        [TestMethod]
        public void BuildViewModelTests_IsSelectedIsMaintainedWell()
        {
            var vm = new BuildViewModel();
            var projectNode = new BuildViewTreeDirectoryNodeViewModel() { Header = "Project node" };
            var dir1Node = new BuildViewTreeDirectoryNodeViewModel() { Header = "Directory 1 node" };
            var dir2Node = new BuildViewTreeDirectoryNodeViewModel() { Header = "Directory 2 node" };
            var fileNode = new BuildViewTreeFileNodeViewModel() { Header = "File 1 node" };
            vm.RootNodes.Add(projectNode);
            projectNode.Children.Add(dir1Node);
            projectNode.Children.Add(dir2Node);
            dir2Node.Children.Add(fileNode);

            dir1Node.IsSelected = true;
            Assert.AreSame(dir1Node, vm.SelectedNode);

            dir2Node.IsSelected = true;
            Assert.AreSame(dir2Node, vm.SelectedNode);

            projectNode.IsSelected = true;
            Assert.AreSame(projectNode, vm.SelectedNode);

            fileNode.IsSelected = true;
            Assert.AreSame(fileNode, vm.SelectedNode);
        }

        [TestMethod]
        public void BuildViewModelTests_Add_new_directory()
        {
            var vm = new BuildViewModel();
            var projectNode = new BuildViewTreeDirectoryNodeViewModel() { Header = "Project node" };
            vm.RootNodes.Add(projectNode);

            projectNode.IsSelected = true;
            vm.CreateDirectoryCommand.Execute(null);

            Assert.AreEqual(1, projectNode.Children.Count);
            Assert.AreEqual("New Folder", projectNode.Children[0].Header);
        }

        [TestMethod]
        public void BuildViewModelTests_Add_new_directory_doesnt_crash_when_selected_is_null()
        {
            var vm = new BuildViewModel();
            var projectNode = new BuildViewTreeDirectoryNodeViewModel() { Header = "Project node" };
            vm.RootNodes.Add(projectNode);
            vm.CreateDirectoryCommand.Execute(null);
        }

        [TestMethod]
        public void BuildViewModelTests_Add_existing_directory()
        {
            var vm = new BuildViewModel();
            var projectNode = new BuildViewTreeDirectoryNodeViewModel() { Header = "Project node" };
            var existingDirectoryName=  Guid.NewGuid().ToString();
            var tempDirectoryPath = Path.Combine(Path.GetTempPath(), existingDirectoryName);
            Directory.CreateDirectory(tempDirectoryPath);
            string tempFileOnePath = Path.Combine(tempDirectoryPath, "File1.txt");
            string tempFileOneText = "This is the content of the first file";
            File.WriteAllText(Path.Combine(tempDirectoryPath, tempFileOnePath), tempFileOneText);
            string tempFileTwoPath = Path.Combine(tempDirectoryPath, "File2.txt");
            string tempFileTwoText = "This is the content of the second file";
            File.WriteAllText(Path.Combine(tempDirectoryPath, tempFileTwoPath), tempFileTwoText);
            vm.RootNodes.Add(projectNode);

            projectNode.IsSelected = true;
            vm.AddExistingDirectoryCommand.Execute(tempDirectoryPath);

            Assert.AreEqual(1, projectNode.Children.Count);
            var directoryNode = projectNode.Children[0] as BuildViewTreeDirectoryNodeViewModel;
            Assert.IsNotNull(directoryNode);
            Assert.AreEqual(existingDirectoryName, directoryNode.Header);

            Assert.AreEqual(2, directoryNode.Children.Count);
            Assert.AreEqual("File1.txt", directoryNode.Children[0].Header);
            Assert.AreEqual("File2.txt", directoryNode.Children[1].Header);
        }

        [TestMethod]
        public void BuildViewModelTests_Add_existing_file()
        {
            var vm = new BuildViewModel();
            var projectNode = new BuildViewTreeDirectoryNodeViewModel() { Header = "Project node" };
            var existingDirectoryName = Guid.NewGuid().ToString();
            var tempDirectoryPath = Path.Combine(Path.GetTempPath(), existingDirectoryName);
            var tempFileOneName = "File1.txt";
            var tempFileOnePath = Path.Combine(tempDirectoryPath, tempFileOneName);
            var tempFileOneText = "This is the content of the first file";
            Directory.CreateDirectory(tempDirectoryPath);
            File.WriteAllText(Path.Combine(tempDirectoryPath, tempFileOnePath), tempFileOneText);
            vm.RootNodes.Add(projectNode);

            projectNode.IsSelected = true;
            vm.AddExistingFileCommand.Execute(tempFileOnePath);

            Assert.AreEqual(1, projectNode.Children.Count);
            var fileNode = projectNode.Children[0] as BuildViewTreeFileNodeViewModel;
            Assert.IsNotNull(fileNode);
            Assert.AreEqual(tempFileOneName, fileNode.Header);
        }
    }
}
